package com.sailmi.auth.userproxy;
import com.sailmi.api.IUserProxyClient;
import com.sailmi.auth.config.UserConfig;
import com.sailmi.common.model.auth.AuthUser;
import com.sailmi.common.secure.user.IUserProvider;
import com.sailmi.common.tool.api.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class UserProvider implements IUserProvider {
	private ApplicationContext appContext;
	private static final Logger log = LoggerFactory.getLogger(UserProvider.class);
    IUserProxyClient userClient;
	UserConfig userConfig;
	public UserProvider(ApplicationContext appContext, UserConfig userConfig) {
		this.appContext = appContext;
		this.userConfig = userConfig;
		userClient =new FeignClientBuilder(appContext)
			.forType(IUserProxyClient.class, userConfig.getUserProxyServiceUrl())
			.build();
	}


    public AuthUser userInfo(Long userId) {
        log.debug("Now get user info "+userId );
		R<AuthUser> response = userClient.userInfo(userId);
		if(Objects.nonNull(response)){
			return response.getData();
		}else{
			return null;
		}
    }

	@Override
	public AuthUser userInfo(String userAccount) {
		return null;
	}

	@Override
	public AuthUser registerUser(AuthUser authUser) {
		//return null;
		return userClient.registerUser(authUser).getData();
	}

	@Override
	public AuthUser updateUser(AuthUser authUser) {
		return null;
	}

	public AuthUser userInfo(String tenantId, String account, String password) {
        log.info("Now in UserProvider for sailmi................................."+account);
		R<AuthUser> res = userClient.userInfo(tenantId,account, password);
		if(Objects.nonNull(res)){
			return res.getData();
		}
		return null;
    }

    public Long submitUserInfo(AuthUser user) {
        return null;
    }

    public Boolean resetUserPass(String id) {
        return null;
    }
}
